package cn.rxmind.dlicense.core.factory;

import cn.rxmind.dlicense.core.constants.DLicenseConstants;
import de.schlichtherle.license.*;

import java.util.prefs.Preferences;

/**
 * 创建LicenseParam
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/13 3:24 下午
 */
public class DLicenseParamFactory {

    private DLicenseParamFactory() {
    }

    /**
     * 创建客户端所需的LicenseParam
     *
     * Class<DefaultLicenseVerifier> clazz = DefaultLicenseVerifier.class;
     *         Preferences preferences = Preferences.userNodeForPackage(clazz);
     *         // 密钥库密码
     *         CipherParam cipherParam = new DefaultCipherParam(configBean.getStorePwd());
     *         KeyStoreParam keyStoreParam
     *                 = new DefaultKeyStoreParam(
     *                 clazz
     *                 , configBean.getPublicStorePath()
     *                 , configBean.getPublicAlias()
     *                 , configBean.getStorePwd(), null);
     *
     *         licenseParam = new DefaultLicenseParam(configBean.getSubject(), preferences, keyStoreParam, cipherParam);
     *
     * @return
     */
    public static LicenseParam createLicenseParam4Client(){

        Class<DLicenseParamFactory> clazz = DLicenseParamFactory.class;
        Preferences preferences = Preferences.userNodeForPackage(clazz);
        // 密钥库密码
        CipherParam cipherParam = new DefaultCipherParam(DLicenseConstants.STORE_PWD);
        KeyStoreParam keyStoreParam
                = new DefaultKeyStoreParam(
                clazz
                , DLicenseConstants.PUBLIC_STORE_PATH
                , DLicenseConstants.PUBLIC_ALIAS
                , DLicenseConstants.STORE_PWD, null);

        return new DefaultLicenseParam(DLicenseConstants.SUBJECT, preferences, keyStoreParam, cipherParam);

    }

    /**
     * 创建服务端所需的LicenseParam
     *
     * Class<LicenseCreator>  clazz = LicenseCreator.class;
     *         Preferences preferences = Preferences.userNodeForPackage(clazz);
     *         // 私钥密码
     *         CipherParam cipherParam = new DefaultCipherParam(licenseConfigBean.getKeyPwd());
     *         // 密钥库信息加载
     *         KeyStoreParam keyStoreParam
     *                 = new DefaultKeyStoreParam(
     *                 clazz                                       // 用于获取加载密钥库文件的类加载器，以加载密钥库文件
     *                 , licenseConfigBean.getPrivateStorePath()   // 密钥库文件路径，privateKey.store
     *                 ,licenseConfigBean.getPrivateAlias()        // 密钥别名       privateKey
     *                 , licenseConfigBean.getStorePwd()           // 密钥库密码,这里应该保证和公钥库密码系统
     *                 , licenseConfigBean.getKeyPwd());           // 密钥密码
     *
     *         LicenseParam licenseParam =
     *                 new DefaultLicenseParam(licenseConfigBean.getSubject(),preferences,keyStoreParam,cipherParam);
     *
     * @return
     */
    public static LicenseParam createLicenseParam4Server(){

        Class<DLicenseParamFactory> clazz = DLicenseParamFactory.class;
        Preferences preferences = Preferences.userNodeForPackage(clazz);
        // 私钥密码
        CipherParam cipherParam = new DefaultCipherParam(DLicenseConstants.KEY_PWD);
        // 密钥库信息加载
        KeyStoreParam keyStoreParam
                = new DefaultKeyStoreParam(
                // 用于获取加载密钥库文件的类加载器，以加载密钥库文件
                clazz
                // 密钥库文件路径，privateKey.store
                , DLicenseConstants.PRIVATE_STORE_PATH
                // 密钥别名       privateKey
                , DLicenseConstants.PRIVATE_ALIAS
                // 密钥库密码,这里应该保证和公钥库密码系统
                , DLicenseConstants.STORE_PWD
                // 密钥密码
                , DLicenseConstants.KEY_PWD);

        LicenseParam licenseParam =
                new DefaultLicenseParam(DLicenseConstants.SUBJECT,preferences,keyStoreParam,cipherParam);

        return licenseParam;
    }
}
